package de.baumann.browser.activitys.user

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import de.baumann.browser.R
import de.baumann.browser.adapter.EthSelectAdapter
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.utils.Constants
import de.baumann.browser.utils.StringUtil
import de.baumann.browser.utils.web3j.ETHWalletUtils
import de.baumann.browser.utils.web3j.WalletDaoUtils
import kotlinx.android.synthetic.main.activity_bind_eth.*
import kotlinx.android.synthetic.main.include_tool_bar.*
import org.web3j.crypto.WalletUtils

/**
 *@author:
 *@date:2019/7/1 17:48
 *@discription: 设置ETH 地址
 */
class BindETHActivity : BaseEditUserInfoActivity() {
    companion object {
        fun startBindETHActivity(context: Activity, requestCode: Int, oldEthUrl: String) {
            val intent = Intent(context, BindETHActivity::class.java).apply {
                putExtra(Constants.ETH_URL, oldEthUrl)
            }
            context.startActivityForResult(intent, requestCode)
        }

        fun startBindETHActivity(context: Context, oldEthUrl: String) {
            val intent = Intent(context, BindETHActivity::class.java).apply {
                putExtra(Constants.ETH_URL, oldEthUrl)
            }
            context.startActivity(intent)
        }

    }

    override fun getLayout(): Int {
        return R.layout.activity_bind_eth
    }

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        btnBindEth.setOnClickListener {
            var address = content
            if (address.startsWith("odx")) {
                address = "0${address.substring(2)}"
            }

            if (WalletUtils.isValidAddress(address)) {
                presenter.editEthUrl(address)
            } else {
                showShortToast("地址格式错误")
            }
        }
    }

    override fun initData() {
        var ethAddress = intent.getStringExtra("ethUrl")
        if (ethAddress.isEmpty()) {
            ethAddress = "-"
        } else {
            btnBindEth.visibility = View.INVISIBLE
            spEth.visibility = View.INVISIBLE
        }
        tvOldEthUrl.text = ethAddress
        val allWallets = WalletDaoUtils.loadAll()
        if (allWallets != null) {
            val spAdapter = EthSelectAdapter(mContext, allWallets)
            spEth.adapter = spAdapter
        }

    }

    override fun getTitleText(): String {
        return getString(R.string.eth_address)
    }

    override fun getContent(): String {
        val selectData = spEth.selectedItem
        if (selectData != null) {
            val ethWallet = selectData as ETHWallet
            var address = ethWallet.address
            if (address.startsWith("0x")){
                address = "od${address.substring(1)}"
            }
            return address
        }
        return ""
    }

    override fun editSuccess() {
        setResult(Activity.RESULT_OK, intent.putExtra(Constants.ETH_URL, content))
        finish()
    }

    override fun showBindEth(): Boolean {
        return false;
    }

}
